﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace GUI
{
    public class Window : Form
    {
        public bool hoiThoat;        
        public Window()
        {
            InitializeComponent();
            this.FormClosing += Window_FormClosing;
            hoiThoat = true;
        }

        void Window_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (!hoiThoat)
                return;
            if (MessageBox.Show(this, "Thoát ?", "Thoát", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
                e.Cancel = true;
        }
        
        ~Window()
        {
            this.FormClosing -= Window_FormClosing;
        }

        private void InitializeComponent()
        {
            this.SuspendLayout();
            // 
            // Window
            // 
            this.BackColor = System.Drawing.Color.LightSkyBlue;
            this.ClientSize = new System.Drawing.Size(784, 562);
            this.ControlBox = false;
            this.Font = new System.Drawing.Font("Tahoma", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
            this.Name = "Window";
            this.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent;
            this.Text = "w";
            this.WindowState = System.Windows.Forms.FormWindowState.Maximized;
            this.Load += new System.EventHandler(this.Window_Load);
            this.ResumeLayout(false);

        }

        private void Window_Load(object sender, EventArgs e)
        {

        }
        public bool KiemTraSo(string a)
        {
            bool kq = true;
            for (int i = 0; i < a.Length; i++)
            {
                if (char.IsDigit(a[i]) == false)
                {
                    return false;
                }
            }
            return kq;
        }
        public void SetBackGroundColorOfMDIForm()
        {
            foreach (Control ctl in this.Controls)
            {
                if ((ctl) is MdiClient)

                // If the control is the correct type,
                // change the color.
                {
                    ctl.BackColor = System.Drawing.Color.LightSkyBlue;
                }
            }

        }
        public void XuatExcel(DataGridView dt,string type)
        {
            SaveFileDialog tb = new SaveFileDialog();
            tb.Filter = "xlsx files (*.xlsx)|*.xlsx|xls files (*.xls)|*.xls|All files (*.*)|*.*";
            tb.FilterIndex = 1;
            tb.FileName = type + "_" + DateTime.Now.ToString("ddMMyyyy");
            tb.OverwritePrompt = true;
            if (tb.ShowDialog() == DialogResult.OK)
            {
                Microsoft.Office.Interop.Excel._Application app = new Microsoft.Office.Interop.Excel.Application();
                Microsoft.Office.Interop.Excel._Workbook workbook = app.Workbooks.Add(Type.Missing);
                Microsoft.Office.Interop.Excel._Worksheet worksheet = null;
                app.Visible = true;
                worksheet = workbook.Sheets["Sheet1"];
                worksheet = workbook.ActiveSheet;
                worksheet.Name = type;
                for (int i = 1; i < dt.Columns.Count + 1; i++)
                {

                    worksheet.Cells[1, i] = dt.Columns[i - 1].HeaderText;

                }
                int cot = (int)'A' + dt.Columns.Count;
                var cl = (char)cot;
                Microsoft.Office.Interop.Excel.Range tieude = worksheet.get_Range("A1", cl+"1");
                tieude.Font.Bold = true;
                for (int i = 0; i < dt.Rows.Count ; i++)
                {

                    for (int j = 0; j < dt.Columns.Count; j++)
                    {

                        worksheet.Cells[i + 2, j + 1] = dt.Rows[i].Cells[j].Value.ToString();

                    }

                }
                workbook.SaveAs(tb.FileName, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlExclusive, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
                app.Quit();
            }
        }
     
    }
}
